package com.capri.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.capri.R;
import com.capri.app.NurseHomeApp;
import com.capri.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;

public class PicViewActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private List<String> imageUrls = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_pic_view;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        NurseHomeApp.getInstance().finishActivity(this);
    }

    @Override
    public void setupData() {
        imageUrls = getIntent().getStringArrayListExtra("key_img_urls");
        PicAdapter adapter=new PicAdapter();
        viewpager.setAdapter(adapter);
    }

    class PicAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageUrls.size();
        }

        public PicAdapter() {

        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            String url = imageUrls.get(position);
            PhotoView photoView = new PhotoView(PicViewActivity.this);
            Glide.with(PicViewActivity.this)
                    .load(url)
                    .into(photoView);
            container.addView(photoView);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PicViewActivity.this.finish();
                }
            });
            return photoView;
        }
    }
}
